﻿/// <reference path="../GeneratedArtifacts/viewModel.js" />
/// <reference path="../Scripts/c1ls-4.0.20141.30.js" />

if (typeof c1ls === "undefined") {
    window.location.href = "http://bit.ly/c1ls-nuget";
}

// http://bit.ly/c1ls-grid
//
myapp.GridConditionalImages.WijmoGrid_render = function (element, contentItem) {

    var table = $("<table/>");
    table.appendTo($(element));

    contentItem.value.oncollectionchange = function () {
        var grid = c1ls.getGridContent(table, contentItem);

        var year = grid.FindColumn("Year");
        year.dataFormatString = "d0";

        var value = grid.FindColumn("Value");
        value.dataFormatString = "c0";

        function imageFormatter(args) {
            if (args.row.type & $.wijmo.wijgrid.rowType.data) {
                if (args.$cell[0].cellIndex === 0) {
                    var obj = grid.GetRowEntity(args.row.dataRowIndex);
                    var img = $("<img/>").attr("src", "Flags/" + getPostalCode(obj.FIPS) + ".png");
                    var div = args.$cell.children();
                    var text = div.text();
                    var span = $("<span>");
                    span.text(text);
                    div.empty();
                    div.append(img);
                    div.append(span);
                    div.children().css("vertical-align", "middle");
                    img.css("padding-right", "6px");
                    return true;
                }
            }
            grid.OnCellFormat(args);
        }

        table.wijgrid({
            // initialization
            columns: grid.Columns,
            data: grid.Rows(),

            // editing
            allowEditing: false,
            afterCellUpdate: grid.OnCellUpdate,
            currentCellChanged: grid.OnCellChanged,

            // sorting
            allowSorting: true,
            sorting: grid.Sorting,

            // filtering
            showFilter: false,
            filtering: grid.Filtering,

            // selection
            selectionMode: "singleRow",
            showRowHeader: grid.ShowRowHeader,
            showSelectionOnRender: false,
            cellClicked: grid.OnCellClicked,
            cellStyleFormatter: imageFormatter
        });
    };

    c1ls.renderControl(contentItem);
};

function getPostalCode(fips) {
    switch (fips) {
        case 01: return "AL";
        case 02: return "AK";
        case 04: return "AZ";
        case 05: return "AR";
        case 06: return "CA";
        case 08: return "CO";
        case 09: return "CT";
        case 10: return "DE";
        case 11: return "DC";
        case 12: return "FL";
        case 13: return "GA";
        case 15: return "HI";
        case 16: return "ID";
        case 17: return "IL";
        case 18: return "IN";
        case 19: return "IA";
        case 20: return "KS";
        case 21: return "KY";
        case 22: return "LA";
        case 23: return "ME";
        case 24: return "MD";
        case 25: return "MA";
        case 26: return "MI";
        case 27: return "MN";
        case 28: return "MS";
        case 29: return "MO";
        case 30: return "MT";
        case 31: return "NE";
        case 32: return "NV";
        case 33: return "NH";
        case 34: return "NJ";
        case 35: return "NM";
        case 36: return "NY";
        case 37: return "NC";
        case 38: return "ND";
        case 39: return "OH";
        case 40: return "OK";
        case 41: return "OR";
        case 42: return "PA";
        case 44: return "RI";
        case 45: return "SC";
        case 46: return "SD";
        case 47: return "TN";
        case 48: return "TX";
        case 49: return "UT";
        case 50: return "VT";
        case 51: return "VA";
        case 53: return "WA";
        case 54: return "WV";
        case 55: return "WI";
        case 56: return "WY";
    }
}